package com.dream.view;

import com.dream.main.DreamGameActivity;
import com.dream.product.KeyProduct;

import android.graphics.Canvas;
import android.graphics.Color;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;

public class MainView extends SurfaceView implements Callback {

	private IScenarioView mView;
	private TutarialThread _thread;
	KeyProduct product;
	MainView main;
	DreamGameActivity _activity;
	property property;
	propertyDialog propertyDialog;
	MiddleView middleView;
	LeftView leftView;
	LeftDownView leftDownView;
	RightView rightView;
	RightDownView rightDownView;
	RightDownBottomView rightDownBottomView;
	RightDownBottomView_1 rightDownBottomView_1;
	ViewStatus currenctStatus, lastStatus;


	public MainView(DreamGameActivity _activity) {
		super(_activity);
		this._activity = _activity;
		property=new property(_activity);
		propertyDialog=new propertyDialog(_activity);
		if (mView == null)
			setView(ViewStatus.Middle);
		KeyProduct.currenctView=ViewStatus.Middle;
		getHolder().addCallback(this);
	}

	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		canvas.drawColor(Color.BLACK);
		mView.drawing(canvas);
		property.onDraw(canvas);
		propertyDialog.onDraw(canvas);
		//		if(System.currentTimeMillis() % 2 == 0)
		//			dtv.drawing(canvas, 1);
		//		else 
		//			dtv.drawing(canvas, 0);
	}

	protected void Logic(long time) {
		mView.logic(time);
		if (KeyProduct.currenctView!=currenctStatus) {
			setView(KeyProduct.currenctView);
		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		property.onTouchEvent(event);
		propertyDialog.onTouchEvent(event);
		boolean flag = false;
		flag = mView.onTouchEvent(event);
		return flag;
	}

	//
	private void releaseView(ViewStatus status) {
		switch (status) {
		case Middle:
			middleView.releaseBitmap();
			break;
		case Left:
			leftView.releaseBitmap();
			break;
		case Left_Down:
			leftDownView.releaseBitmap();
			break;
		case Right:
			rightView.releaseBitmap();
			break;
		case Right_Down:
			rightDownView.releaseBitmap();
			break;
		case RightDownBottom:
			rightDownBottomView.releaseBitmap();
			break;
		case RightDownBottom_1:
			rightDownBottomView_1.releaseBitmap();
			break;
		default:
			break;
		}
	}
	private void release(ViewStatus status) {
		switch (status) {
		case Middle:
			middleView.release();
			break;
		case Left:
			leftView.release();
			break;
		case Left_Down:
			leftDownView.release();
			break;
		case Right:
			rightView.release();
			break;
		case Right_Down:
			rightDownView.release();
			break;
		case RightDownBottom:
			rightDownBottomView.release();
			break;
		case RightDownBottom_1:
			rightDownBottomView_1.release();
			break;
		default:
			break;
		}
	}

	private void setView(ViewStatus status) {
		switch (status) {
		case Middle:
			middleView = new MiddleView(_activity);
			mView = middleView;
			if (currenctStatus!=null) {
				lastStatus = currenctStatus;
			}
			currenctStatus = status;
			if(lastStatus != null){
				releaseView(lastStatus);
				release(lastStatus);
			};
			break;
		case Left:
			leftView = new LeftView(_activity);
			mView = leftView;
			lastStatus = currenctStatus;
			currenctStatus = status;
			if(lastStatus != null){
				releaseView(lastStatus);
				release(lastStatus);
			};
			break;
		case Left_Down:
			leftDownView = new LeftDownView(_activity);
			mView = leftDownView;
			lastStatus = currenctStatus;
			currenctStatus = status;
			if(lastStatus != null){
				releaseView(lastStatus);
				release(lastStatus);
			};
			break;
		case Right:
			rightView = new RightView(_activity);
			mView = rightView;
			lastStatus = currenctStatus;
			currenctStatus = status;
			if(lastStatus != null){
				releaseView(lastStatus);
				release(lastStatus);
			};
			break;
		case Right_Down:
			rightDownView = new RightDownView(_activity);
			mView = rightDownView;
			lastStatus = currenctStatus;
			currenctStatus = status;
			if(lastStatus != null){
				releaseView(lastStatus);
				release(lastStatus);
			};
			break;
		case RightDownBottom:
			rightDownBottomView = new RightDownBottomView(_activity);
			mView = rightDownBottomView;
			lastStatus = currenctStatus;
			currenctStatus = status;
			if(lastStatus != null){
				releaseView(lastStatus);
				release(lastStatus);
			};
			break;
		case RightDownBottom_1:
			rightDownBottomView_1 = new RightDownBottomView_1(_activity);
			mView = rightDownBottomView_1;
			lastStatus = currenctStatus;
			currenctStatus = status;
			if(lastStatus != null){
				releaseView(lastStatus);
				release(lastStatus);
			};
			break;
		case HeadFront:
			// mView = new MiddleView();
			break;
		default:
			break;
		}
	}

	@Override
	public void surfaceCreated(SurfaceHolder sfh) {
		_thread = new TutarialThread(sfh, this);
		_thread.setFlag(true);
		_thread.start();
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {

	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {

	}

	class TutarialThread extends Thread {
		Canvas canvas;
		SurfaceHolder sfh;
		MainView mv;
		boolean isFlag;
		long time;

		public TutarialThread(SurfaceHolder sfh, MainView mv) {
			this.sfh = sfh;
			this.mv = mv;
		}

		public void setFlag(boolean isFlag) {
			this.isFlag = isFlag;
		}

		@Override
		public void run() {
			while (isFlag) {
				time++;
				try {
					canvas = sfh.lockCanvas();
					synchronized (sfh) {
						mv.onDraw(canvas);
						mv.Logic(time);
					}
				} finally {
					if (canvas != null) {
						sfh.unlockCanvasAndPost(canvas);
					}
				}
				try {
					sleep(60);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
